#include "BackendMiddlewarePocoServer.h"

BackendMiddlewarePocoServer::BackendMiddlewarePocoServer() {
    serverMng = ServerManagerPtr(new ServerManager());
    logger = &Logger::get("BackendMiddlewarePocoServer");

}

BackendMiddlewarePocoServer::BackendMiddlewarePocoServer(const BackendMiddlewarePocoServer& orig) {

}

BackendMiddlewarePocoServer::~BackendMiddlewarePocoServer() {

}

int BackendMiddlewarePocoServer::main(const std::vector<std::string>& args) {
    int port = this->config().getInt("port");
    // Create server

    //    std::string host = this->config().getString("host");
    //    std::cout << "Port = " << port << "\n";
    //    std::cout << "Host = " << host << "\n";
    //    TNonblockingServer server = serverMng->createConnection(port);
    //    serverMng->listenning(server);
    //    waitForTerminationRequest();
    string path = getPathDatabase();
    path.append("lazyboys.kch");
    //string path = "./database/lazyboys.kch";
    
    dbManager.copyDBFromHashDBtoGrassDB(path, grassDB);
    DB::Cursor* cur = grassDB.cursor();
    cur->jump();
    string ckey, cvalue;

    while (cur->get(&ckey, &cvalue, true)) {
        cout << ckey << " : " << cvalue << endl;
    }
    return 0;
}

void BackendMiddlewarePocoServer::initialize(Application& self) {
    loadConfiguration();
    BaseClass::initialize(self);
}

void BackendMiddlewarePocoServer::reinitialize(Application& self) {
    BaseClass::reinitialize(self);
}

void BackendMiddlewarePocoServer::uninitialize() {
    BaseClass::uninitialize();
}

void BackendMiddlewarePocoServer::defineOptions(OptionSet& options) {
    BaseClass::defineOptions(options);
    options.addOption(Option("help", "h", "display help information")
            .required(false)
            .repeatable(false));
    Option().callback(OptionCallback<BackendMiddlewarePocoServer > (this, &BackendMiddlewarePocoServer::handleOption));
}

void BackendMiddlewarePocoServer::handleOption(const std::string& name, const std::string& value) {
    if (name == "help") {
        displayHelp();
        stopOptionsProcessing();
    } else {
        BaseClass::handleOption(name, value);
    }
}

void BackendMiddlewarePocoServer::displayHelp() {
    HelpFormatter helpFormatter(options());
    helpFormatter.setCommand(commandName());
    helpFormatter.setUsage("[options]");
    helpFormatter.setHeader("options:");
    helpFormatter.format(std::cout);
}

string BackendMiddlewarePocoServer::getPathDatabase() {
    return this->config().getString("pathDatabase");
}
POCO_SERVER_MAIN(BackendMiddlewarePocoServer);